// const { isString, cloneDeep } = require('lodash');
const sdk = require('./sdk');
// 获取某个commit的具体信息
module.exports.get = async function (data) {
  // console.log(`---------获取单个commit信息 : ${JSON.stringify(data.data)}`);
  const { query } = data.data;
  data.data.json = {
    DepotId: query.project_id,
    Sha: query.commit_id,
    Action: 'DescribeGitCommitInfo',
  };

  const commitRes = await sdk.request('/DescribeGitCommitInfo', 'POST', data);
  // 接口返回的格式如下
  // {
  //   ShortMessage: 'Initial Commit',
  //   Sha: 'c8c9d86a178d239eaf39622f60cf150d1edcea9f',
  //   CommitDate: 1731391761000,
  //   Committer: { Email: 'li.sensen@joosure.com', Name: '李森森' },
  //   CreatedAt: 1731391761000,
  //   AuthorName: '李森森',
  //   AuthorEmail: 'li.sensen@joosure.com',
  //   Parents: []
  // }
  // console.log('-------------查询到的commit信息: ', commitRes.Response.GitCommit);
  const commit = commitRes.Response.GitCommit;
  if (commit) {
    commit.unique_commit_id = `${data.data.query.project_id}-${commit.id}`;
  }
  // 拼接commit url
  data.data.json.Action = 'DescribeGitDepot';
  const projectInfo = await sdk.request('/DescribeGitDepot', 'POST', data);
  // console.log('---------------获取到的仓库信息projectInfo: ', projectInfo);
  const resp = {
    id: commit.Sha,
    message: commit.ShortMessage,
    parent_ids: commit.Parents,
    // authored_date: '2024-11-13T02:29:23+0000',
    author_name: commit.AuthorName,
    author_email: commit.AuthorEmail,
    // committed_date: commit.CommitDate,
    committer_name: commit.Committer.Name,
    committer_email: commit.Committer.Email,
    title: commit.ShortMessage,
    scroll_object_id: null,
    // created_at: commit.CreatedAt,
    short_id: commit.Sha,
    unique_commit_id: `${query.project_id}-${commit.Sha}`,
    path: `${projectInfo?.Response?.Depot?.WebUrl}/commit/${commit.Sha}`,
    authored_date: new Date(commit.CommitDate).getTime(),
    committed_date: new Date(commit.CommitDate).getTime(),
    created_at: new Date(commit.CommitDate).getTime(),
  };
  console.log('-----------------最终提交的数据resp-单个commit信息: ', resp);
  return resp;
};
// 获取某个commit信息的修改内容
module.exports.diff = async function (data) {
  // console.log(`execute commit.diff : ${JSON.stringify(data.data)}`);
  const { query } = data.data;
  data.data.json = {
    DepotId: query.project_id,
    Sha: query.commit_id,
    Action: 'DescribeGitCommitDiff',
  };
  const diffRes = await sdk.request('/DescribeGitCommitDiff', 'POST', data);
  const finalRes = diffRes?.Response?.Diffs?.map(one => ({
    old_path: one.Path,
    new_path: one.Path,
    a_mode: one.NewMode,
    b_mode: one.OldMode,
    diff: one.Content,
    additions: one.Insertions,
    deletions: one.Deletions,
    new_file: false,
    renamed_file: false,
    deleted_file: false,
    is_too_large: false,
    is_collapse: false,
  }));
  console.log('---------------------------------diffRes?.Response?.Diffs: ', diffRes?.Response?.Diffs);
  console.log('finalRes: ', finalRes);
  return finalRes;
};
// 获取某个仓库的master分支的提交
module.exports.query = async function (data) {
  // console.log(`execute commit.query : ${JSON.stringify(data.data.query)}`);
  const { query } = data.data;
  // console.log('--------查询commitquery: ', JSON.stringify(query));
  // TODO 查询仓库的提交，目前只能查找固定分支，之后这里可以查询当前仓库的默认分支，然后赋值到Ref上，文档地址  https://coding.net/help/openapi#/operations/DescribeDepotDefaultBranch
  data.data.json = {
    ...data.data.json,
    DepotId: query.project_id,
    // EndDate: query.end_date,
    // :author
    Ref: 'master', // 分支名称-只能设置一个分支
    PageSize: 999999,
    PageNumber: 1,
    // StartDate: new Date(query.begin_date).getTime(), // 不支持时间搜索
    // KeyWord: query.message, // 暂不支持搜索
    Action: 'DescribeGitCommitInfos',
  };
  const rsData = await sdk.request('/DescribeGitCommitInfos', 'POST', data);
  console.log('-------获取到的当前仓库下的master分支下的提交记录rsData', rsData?.Response?.Commits?.length);
  const res = rsData?.Response?.Commits?.map(one => ({
    commit: one.Sha,
    username: one.Committer.Name,
    message: one.ShortMessage,
    time: new Date(one.CommitDate).getTime(),
  }));
  return res;
};
